package com.only4play.pcc.rest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author liyuncong
 * @version 1.0
 * @file RestExceptionHandler
 * @brief RestExceptionHandler
 * @details RestExceptionHandler
 * @date 2024-06-24
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-24               liyuncong          Created
 */

@Slf4j
@RestControllerAdvice
public class RestExceptionHandler {

    private static final int ERROR_CODE = -1;
    private static final String ERROR_MESSAGE = "Some exception happened";

    @ExceptionHandler(value = {Exception.class, Throwable.class})
    public GenericResponse<?> handler(Exception exception) {
        String errorMessage = exception.getMessage();
        if (!StringUtils.hasText(errorMessage)) {
            errorMessage = ERROR_MESSAGE;
        }

        log.error("exception handle:", exception);
        log.error("[全局异常拦截]Exception Message:  {}", errorMessage);

        return GenericResponse.of(ERROR_CODE, errorMessage, null);
    }
}
